exports.__esModule = true;

var _moment = require('moment');

var _moment2 = _interopRequireDefault(_moment);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.BaseSocket {

    /**
     * 自定义表单首页列表
     * @returns
     */
    getHomeFormListAction() {
        var _this2 = this;

        return _asyncToGenerator(function* () {
            let aj = think.ajaxJson();
            let user = _this2.getUser();
            const BpmnService = _this2.service('api/bpmn/BpmnService');
            const res = yield BpmnService.getHomeFormList(user);
            if (!res.error) {
                aj.result = res;
            } else {
                aj.success = false;
                aj.msg = '查询失败';
            }
            return _this2.json(aj);
        })();
    }
    /**
     * 自定义表单首页列表
     * @returns
     */
    getFormInfoAction() {
        var _this3 = this;

        return _asyncToGenerator(function* () {
            let aj = think.ajaxJson();
            let id = _this3.post('id');
            const BpmnService = _this3.service('api/bpmn/BpmnService');
            const res = yield BpmnService.getFormInfo(id);
            if (!res.error) {
                aj.result = res;
            } else {
                aj.success = false;
                aj.msg = '查询失败';
            }
            return _this3.json(aj);
        })();
    }
    /**
     * 查询待我审批和我已审批列表
     * @returns
     */
    getMyApproveAction() {
        var _this4 = this;

        return _asyncToGenerator(function* () {
            let aj = think.ajaxJson();
            let sourceUserId = _this4.post('sourceUserId');
            let targetUserId = _this4.post('targetUserId');
            let label = _this4.post('label');
            let page = _this4.post('page');
            let type = _this4.post('type');
            let applyStatus = _this4.post('applyStatus');
            let keywords = _this4.post('keywords');
            let pageSize = _this4.post('pageSize');
            const BpmnService = _this4.service('api/bpmn/BpmnService');
            const res = yield BpmnService.getMyApprove(sourceUserId, targetUserId, keywords, label, type, applyStatus, page, pageSize);
            if (!res.error) {
                aj.result = res;
            } else {
                aj.success = false;
                aj.msg = '查询失败';
            }
            return _this4.json(aj);
        })();
    }
    /**
     *表单提交
     * @returns
     */
    submitFormAction() {
        var _this5 = this;

        return _asyncToGenerator(function* () {
            let aj = think.ajaxJson();
            const BpmnService = _this5.service('api/bpmn/BpmnService');
            let formId = _this5.post('formId');
            let userId = _this5.post('userId');
            let data = _this5.post('data');

            const res = yield BpmnService.submitForm(userId, formId, data);
            if (!res.error) {
                _this5.sendMsgByUserAction(res.socketBody, res.userIds, 'control_center');
                _this5.sendMsgByUserAction(res.socketBody, res.userIds, 'msg_box');
            } else {
                aj.success = false;
                aj.msg = '提交失败';
            }
        })();
    }
    /**
     *查询审批人
     * @returns
     */
    getApproverAction() {
        var _this6 = this;

        return _asyncToGenerator(function* () {
            let aj = think.ajaxJson();
            const BpmnService = _this6.service('api/bpmn/BpmnService');
            let formId = _this6.post('formId');
            const res = yield BpmnService.getApprover(formId);
            if (!res.error) {
                aj.result = res;
            } else {
                aj.success = false;
                aj.msg = '查询失败';
            }
            return _this6.json(aj);
        })();
    }

    /**
     * 通过porcid查询审批信息
     * @returns
     */
    approveInfoAction() {
        var _this7 = this;

        return _asyncToGenerator(function* () {
            let aj = think.ajaxJson();
            const BpmnService = _this7.service('api/bpmn/BpmnService');
            let proc_inst_id = _this7.post('id');
            const res = yield BpmnService.approveInfo(proc_inst_id);
            if (!res.error) {
                aj.result = res;
            } else {
                aj.success = false;
                aj.msg = '查询失败';
            }
            return _this7.json(aj);
        })();
    }

    /**
     * 通过porcid查询案件流程信息
     * @param {any} data  data.id
     * @returns
     */
    taskMsgListAction() {
        var _this8 = this;

        return _asyncToGenerator(function* () {
            let aj = think.ajaxJson();
            let proc_inst_id = _this8.post('id');
            const BpmnService = _this8.service('api/bpmn/BpmnService');
            const res = yield BpmnService.getTaskByProcId(proc_inst_id);
            if (!res.error) {
                aj.result = res;
            } else {
                aj.success = false;
                aj.msg = '查询失败';
            }
            return _this8.json(aj);
        })();
    }
    /**
     * 通过porcid查询案件流程flow信息
     * @param {any} data  data.id
     * @returns
     */
    flowMsgListAction() {
        var _this9 = this;

        return _asyncToGenerator(function* () {
            let aj = think.ajaxJson();
            let proc_inst_id = _this9.post('id');
            let userId = _this9.post('userId');
            const BpmnService = _this9.service('api/bpmn/BpmnService');
            const res = yield BpmnService.flowMsgList(proc_inst_id, userId);
            if (!res.error) {
                aj.result = res;
            } else {
                aj.success = false;
                aj.msg = '查询失败';
            }
            return _this9.json(aj);
        })();
    }

    /**
     *流程表单提交
     * @returns
     */
    submitFlowFormAction() {
        var _this10 = this;

        return _asyncToGenerator(function* () {
            let aj = think.ajaxJson();
            const BpmnService = _this10.service('api/bpmn/BpmnService');
            let flowId = _this10.post('flowId');
            let userId = _this10.post('userId');
            let procInstId = _this10.post('procInstId');
            let option = _this10.post('option');
            let uploadImgs = _this10.post('uploadImgs');
            let approveType = _this10.post('approveType');
            try {
                const res = yield BpmnService.submitFlowForm(flowId, option, userId, procInstId, uploadImgs, approveType);
                if (!res.error) {
                    if (res.state == 1) {
                        aj.success = false;
                        aj.msg = '该审批已被其他人审批！';
                        aj.state = res.state;
                        return _this10.json(aj);
                    }
                    aj.state = res.state;
                    if (res.backObj.userIds.length > 0) {
                        let socketBody = _this10.getSocketMsgBody();
                        socketBody.result = res.backObj.socketBody.result;
                        socketBody.title = res.backObj.socketBody.title;
                        socketBody.sendUser = userId;
                        socketBody.menuUrl = 'controlCenter';
                        _this10.sendMsgByUserAction(socketBody, res.backObj.userIds, 'control_center');
                        socketBody.menuUrl = 'myTask';
                        _this10.sendMsgByUserAction(socketBody, res.backObj.userIds, 'msg_box');
                    }
                    let socketBody1 = _this10.getSocketMsgBody();
                    socketBody1.result = res.createObj.socketBody.result;
                    socketBody1.sendUser = userId;
                    socketBody1.title = res.createObj.socketBody.title;
                    socketBody1.menuUrl = 'approve';
                    _this10.sendMsgByUserAction(socketBody1, res.createObj.userIds, 'msg_box');
                    if (res.nextActinstIds.length > 0) {
                        aj.result.selectPerson = true;
                        aj.result.id = res.nextActinstIds[0];
                    } else {
                        aj.result.selectPerson = false;
                        aj.result.id = '';
                    }
                } else {
                    aj.success = false;
                    aj.msg = '查询失败';
                }
                return _this10.json(aj);
            } catch (e) {}
        })();
    }

    /**
     *事件id查询流程详情列表
     * @returns
     */
    queryUserTaskByEventIDAction() {
        var _this11 = this;

        return _asyncToGenerator(function* () {
            let aj = think.ajaxJson();
            const BpmnService = _this11.service('api/bpmn/BpmnService');
            let id = _this11.post('id');
            try {
                const res = yield BpmnService.queryUserTaskByEventID(id);
                if (!res.error) {
                    aj.msg = '查询成功';
                    aj.result = res;
                } else {
                    aj.success = false;
                    aj.msg = '查询失败';
                }
                return _this11.json(aj);
            } catch (e) {}
        })();
    }

    /**
     *修改流程审批人
     * @returns
     */
    setApprovePersonAction() {
        var _this12 = this;

        return _asyncToGenerator(function* () {
            let aj = think.ajaxJson();
            let _this = _this12;
            const BpmnService = _this12.service('api/bpmn/BpmnService');
            let id = _this12.post('id');
            let userid = _this12.post('userId');
            let createId = _this12.getUser().userId;
            try {
                const res = yield BpmnService.updateApprovePerson(id, userid, createId);
                if (!res.error) {
                    let socketBody = _this12.getSocketMsgBody();
                    socketBody.result = res.socketBody.result;
                    socketBody.title = res.socketBody.title;
                    socketBody.sendUser = createId;
                    socketBody.sendDate = (0, _moment2.default)().format("YYYY-MM-DD HH:mm:ss");
                    socketBody.menuUrl = 'myTask';
                    _this12.sendMsgByUserAction(socketBody, res.userIds, 'msg_box');
                    _this12.sendMsgByUserAction(socketBody, res.userIds, 'control_center');
                    aj.msg = '修改成功';
                } else {
                    aj.success = false;
                    aj.msg = '修改失败';
                }
                return _this12.json(aj);
            } catch (e) {
                aj.success = false;
                aj.msg = '修改失败';
            }
        })();
    }

    // //获取所有组织机构和人员
    // async orgAndUserAction() {
    //     let aj = think.ajaxJson();
    //     let data = this.get();
    //     const BpmnService = this.service('api/bpmn/BpmnService');
    //     const result = await BpmnService.getOrgAndUser(data);
    //     if (!result.error) {
    //         aj.result = result
    //
    //     } else {
    //         aj.success = false;
    //         aj.msg = '查询失败'
    //     }
    //     return this.json(aj)
    // }
};